/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.company.management.controller;

import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import org.company.management.entity.EmployeeSchedule;
import org.company.management.entity.EmployeeScheduleInfo;
import org.company.management.controller.exceptions.NonexistentEntityException;

/**
 *
 * @author Paner
 */
@SuppressWarnings("serial")
public class EmployeeScheduleInfoJpaController implements Serializable {

    public EmployeeScheduleInfoJpaController() {
    	emf = Persistence.createEntityManagerFactory("Management");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(EmployeeScheduleInfo employeeScheduleInfo) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            EmployeeSchedule employeescheduleid = employeeScheduleInfo.getEmployeescheduleid();
            if (employeescheduleid != null) {
                employeescheduleid = em.getReference(employeescheduleid.getClass(), employeescheduleid.getId());
                employeeScheduleInfo.setEmployeescheduleid(employeescheduleid);
            }
            em.persist(employeeScheduleInfo);
            if (employeescheduleid != null) {
                employeescheduleid.getEmployeeScheduleInfoList().add(employeeScheduleInfo);
                employeescheduleid = em.merge(employeescheduleid);
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(EmployeeScheduleInfo employeeScheduleInfo) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            EmployeeScheduleInfo persistentEmployeeScheduleInfo = em.find(EmployeeScheduleInfo.class, employeeScheduleInfo.getId());
            EmployeeSchedule employeescheduleidOld = persistentEmployeeScheduleInfo.getEmployeescheduleid();
            EmployeeSchedule employeescheduleidNew = employeeScheduleInfo.getEmployeescheduleid();
            if (employeescheduleidNew != null) {
                employeescheduleidNew = em.getReference(employeescheduleidNew.getClass(), employeescheduleidNew.getId());
                employeeScheduleInfo.setEmployeescheduleid(employeescheduleidNew);
            }
            employeeScheduleInfo = em.merge(employeeScheduleInfo);
            if (employeescheduleidOld != null && !employeescheduleidOld.equals(employeescheduleidNew)) {
                employeescheduleidOld.getEmployeeScheduleInfoList().remove(employeeScheduleInfo);
                employeescheduleidOld = em.merge(employeescheduleidOld);
            }
            if (employeescheduleidNew != null && !employeescheduleidNew.equals(employeescheduleidOld)) {
                employeescheduleidNew.getEmployeeScheduleInfoList().add(employeeScheduleInfo);
                employeescheduleidNew = em.merge(employeescheduleidNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = employeeScheduleInfo.getId();
                if (findEmployeeScheduleInfo(id) == null) {
                    throw new NonexistentEntityException("The employeeScheduleInfo with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            EmployeeScheduleInfo employeeScheduleInfo;
            try {
                employeeScheduleInfo = em.getReference(EmployeeScheduleInfo.class, id);
                employeeScheduleInfo.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The employeeScheduleInfo with id " + id + " no longer exists.", enfe);
            }
            EmployeeSchedule employeescheduleid = employeeScheduleInfo.getEmployeescheduleid();
            if (employeescheduleid != null) {
                employeescheduleid.getEmployeeScheduleInfoList().remove(employeeScheduleInfo);
                employeescheduleid = em.merge(employeescheduleid);
            }
            em.remove(employeeScheduleInfo);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<EmployeeScheduleInfo> findEmployeeScheduleInfoEntities() {
        return findEmployeeScheduleInfoEntities(true, -1, -1);
    }

    public List<EmployeeScheduleInfo> findEmployeeScheduleInfoEntities(int maxResults, int firstResult) {
        return findEmployeeScheduleInfoEntities(false, maxResults, firstResult);
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
	private List<EmployeeScheduleInfo> findEmployeeScheduleInfoEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(EmployeeScheduleInfo.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public EmployeeScheduleInfo findEmployeeScheduleInfo(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(EmployeeScheduleInfo.class, id);
        } finally {
            em.close();
        }
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
	public int getEmployeeScheduleInfoCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<EmployeeScheduleInfo> rt = cq.from(EmployeeScheduleInfo.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
